﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using SaleSoft.Models;
using SaleSoft.Helper;

namespace SaleSoft.Areas.administrator.Controllers
{
    public class NhanVienQuanLyController : BaseController {
        #region Properties Section
        private SaleSoftEntities db = new SaleSoftEntities();
        #endregion

        #region Views Section
        public ViewResult Index()
        {
            var ds_nhanvienquanly = db.ds_nhanvienquanly.Include("ds_nhomquantri");
            return View(ds_nhanvienquanly.ToList());
        }

        public ActionResult Create()
        {
            ViewBag.id_nhomquantri = SelectListHelper.DSNhomQuanTri(null);
            ViewBag.id_trangthai = SelectListHelper.DSTrangThai(2, null);
            return View();
        }

        public ActionResult Edit ( int id ) {
            ds_nhanvienquanly ds_nhanvienquanly = db.ds_nhanvienquanly.Single(d => d.id_nhanvien == id);
            ViewBag.id_nhomquantri = SelectListHelper.DSNhomQuanTri(ds_nhanvienquanly.id_nhanvien);
            ViewBag.id_trangthai = SelectListHelper.DSTrangThai(2, ds_nhanvienquanly.id_trangthai);
            return View(ds_nhanvienquanly);
        }
        #endregion

        #region CRUD Section

        [HttpPost]
        public ActionResult Create(ds_nhanvienquanly ds_nhanvienquanly)
        {
            if (ModelState.IsValid)
            {
                db.ds_nhanvienquanly.AddObject(ds_nhanvienquanly);
                db.SaveChanges();
                return RedirectToAction("Index");  
            }
            ViewBag.id_trangthai = SelectListHelper.DSTrangThai(2, ds_nhanvienquanly.id_trangthai);
            ViewBag.id_nhomquantri = SelectListHelper.DSNhomQuanTri(ds_nhanvienquanly.id_nhanvien);
            return View(ds_nhanvienquanly);
        }

        [HttpPost]
        public ActionResult Edit ( ds_nhanvienquanly ds_nhanvienquanly ) {
            if (ModelState.IsValid) {
                db.ds_nhanvienquanly.Attach(ds_nhanvienquanly);
                db.ObjectStateManager.ChangeObjectState(ds_nhanvienquanly, EntityState.Modified);
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.id_trangthai = SelectListHelper.DSTrangThai(2, ds_nhanvienquanly.id_trangthai);
            ViewBag.id_nhomquantri = ViewBag.id_nhomquantri = SelectListHelper.DSNhomQuanTri(ds_nhanvienquanly.id_nhanvien);
            return View(ds_nhanvienquanly);
        }

        public ActionResult Delete ( int id ) {
            ds_nhanvienquanly ds_nhanvienquanly = db.ds_nhanvienquanly.Single(d => d.id_nhanvien == id);
            db.ds_nhanvienquanly.DeleteObject(ds_nhanvienquanly);
            db.SaveChanges();
            return RedirectToAction("index");
        }
        #endregion

        #region Additional Section
        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
        #endregion
    }
}